﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;
using System.Web;

namespace NetOpen_System.Component
{
    /// <summary>
    /// Url匹配条件
    /// </summary>
    public sealed class UrlMatchCondition
    {
        private string _pattern;
        private Regex _regex;

        public UrlMatchCondition(string pattern)
        {
            this._pattern = pattern;
        }


        public bool IsMatch(string rawUrl)
        {
            if (this._regex == null)
            {
                this._regex = new Regex(ResolveLocation(this.Pattern), RegexOptions.IgnoreCase);
            }
            if (string.IsNullOrEmpty(rawUrl))
                rawUrl = HttpContext.Current.Request.RawUrl;

            Match match = this._regex.Match(rawUrl);
            return match.Success;
        }

        public string Pattern
        {
            get
            {
                return this._pattern;
            }
            set
            {
                this._pattern = value;
            }
        }


        public static string ResolveLocation(string location)
        {
            string applicationPath = HttpContext.Current.Request.ApplicationPath;
            if (applicationPath.Length > 1)
            {
                applicationPath = applicationPath + "/";
            }
            return location.Replace("~/", applicationPath);
        }






    }
}
